package com.pswv2.backstage.timing;

import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.pswv2.bub.bsc.dao.po.AnswerRequirements;
import com.pswv2.bub.bsc.dao.po.DataManagement;
import com.pswv2.bub.bsc.dao.po.MyWallet;
import com.pswv2.bub.bsc.dao.po.User;
import com.pswv2.bub.bsc.dao.po.UserRequirements;
import com.pswv2.bub.bsc.dao.po.UserTerritory;
import com.pswv2.bub.bsc.dao.po.UserUserRequirements;
import com.pswv2.util.ExtraSpringHibernateTemplate;


@Component("Timer")
public class Timer {
	private static Class<?> pocDM = DataManagement.class;
	private static Class<?> pocUUR = UserUserRequirements.class;
	private static Class<?> pocMW = MyWallet.class;
	private static Class<?> pocUT = UserTerritory.class;
	private static Class<?> pocA = AnswerRequirements.class;
	private static Class<?> pocU = User.class;
	private static Class<?> poc = UserRequirements.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Scheduled
	public void show(){
		List<UserRequirements> lsur=(List<UserRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From UserRequirements where status=? or status=?", 1,3);
		for(UserRequirements ur:lsur){
			long date = new Date(System.currentTimeMillis()).getTime();
			long ss= ur.getReleaseTime();
            int fulfilTime= ur.getFulfilTime();
            long ss1=fulfilTime*(1000 * 60 * 60 * 24);
			long diff=ss1+(ss-date);
			if(diff<=0){
				ur.setStatus(ur.status_obsolete_closing_a_case);
				extraSpringHibernateTemplate.getHibernateTemplate().update(ur);
				MyWallet mw=extraSpringHibernateTemplate.findFirstOneByPropEq(pocMW, "userId", ur.getUserId());
				double money=mw.getMoney();
				List<AnswerRequirements> lsa=(List<AnswerRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From AnswerRequirements where userRequirementsId=? and statusSee=?", ur.getUserRequirementsId(),2);
				List<DataManagement> lsdm=(List<DataManagement>) extraSpringHibernateTemplate.findAll(pocDM);
				DataManagement dm=lsdm.get(0);
				double totalAmountDM=0;
				double totalAmount=0;
				for(AnswerRequirements ar:lsa){
					int aUserId=ar.getUserId();
					double remuneration=ar.getRemunerationPrice();
					totalAmount=totalAmount+remuneration;
					MyWallet mwa=extraSpringHibernateTemplate.findFirstOneByPropEq(pocMW, "userId", aUserId);
					if(mwa==null){
						MyWallet mw1=new MyWallet();
						mw1.setMoney(remuneration*8/10);
						mw1.setUserId(aUserId);
						extraSpringHibernateTemplate.getHibernateTemplate().save(mw1);
					}else{
						double m=mwa.getMoney();
						mwa.setMoney(m+remuneration*8/10);
						extraSpringHibernateTemplate.getHibernateTemplate().update(mwa);
					}
					mw.setMoney(money-totalAmount);
					extraSpringHibernateTemplate.getHibernateTemplate().update(mw);
					totalAmountDM=totalAmountDM+remuneration*2/10;
				}
				totalAmountDM=dm.getAllFund()+totalAmountDM;
				dm.setAllFund(totalAmountDM);
				extraSpringHibernateTemplate.getHibernateTemplate().update(dm);
			}
		}
		System.out.println("Taskaa:sasas");  
	}

}
